package com.siecomzg.tool;


import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;

import com.google.gson.JsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@SuppressWarnings("unused")
public class FileUnits {
    private String SDPath;
    
    public FileUnits(){
        
        SDPath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
    }
    public InputStream readFromSrcPath(String filename){
    	InputStream is= null;
		is =this.getClass().getResourceAsStream("/com/siecomzg/tools/" +filename);
    	return is;
    }
    /**
     * 
     * @param fileName
     * @return
*/
    public File createSDFile(String fileName){
        File file=new File(SDPath+fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
    
    /**
     *
     * @param dirName
     * @return
*/
    public File createSDDir(String dirName){
        File file=new File(SDPath+dirName);
        file.mkdir();
        return file;
    }
    
    /**
     *
     * @param fileName
     * @return
*/
    public boolean isFileExist(String fileName){
        File file=new File(SDPath+fileName);
        if(file.length()==0){
            file.delete();
            return false;
        }
        return file.exists();
    }
    /**
     *
     * @param path
     * @param fileName
     * @param inputStream
     * @return
*/
    public File writeToSDfromInput(String path,String fileName,InputStream inputStream){
        createSDDir(path);
        File file=createSDFile(path+"/"+fileName);
        OutputStream outStream=null;
        try {
            outStream=new FileOutputStream(file);
            byte[] buffer=new byte[4*1024];
            while(inputStream.read(buffer)!=-1){
                outStream.write(buffer);
            }
            outStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                outStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
    public byte[] readSDFile(String path,String fileName) {
		File file = new File(SDPath+path+"/"+fileName);
		byte[] b = null;
		try {
			@SuppressWarnings("resource")
			FileInputStream inputStream = new FileInputStream(file);
            b = new byte[inputStream.available()];
            inputStream.read(b);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return b;
	}
    @SuppressLint("SdCardPath")
	public static void writeFileToSD(String s,String pathName,String fileName) {
        String sdStatus = Environment.getExternalStorageState();  
        if(!sdStatus.equals(Environment.MEDIA_MOUNTED)) {  
            LogUtil.d("TestFile", "SD card is not avaiable/writeable right now.");
            return;  
        }  
        try {  
//            String pathName="/sdcard/test/";
//            String fileName="yyy.txt";
            File path = new File(pathName);
            File file = new File( fileName);
            if( !path.exists()) {  
                LogUtil.d("TestFile", "Create the path:" + pathName);
                path.mkdir();  
            }  
            if( !file.exists()) {  
                LogUtil.d("TestFile", "Create the file:" + fileName);
                file.createNewFile();  
            }  
            FileOutputStream stream = new FileOutputStream(file);  
           
            byte[] buf = s.getBytes();  
           
            stream.write(buf);            
            stream.close();  
              
        } catch(Exception e) {  
            LogUtil.e("TestFile", "Error on writeFilToSD.");
            e.printStackTrace();  
        }  
    }


    public static boolean saveImage(Bitmap bmp,String strSignPath) {
//        File appDir = new File(Environment.getExternalStorageDirectory(), "siecom");
//        if (!appDir.exists()) {
//            appDir.mkdir();
//        }
        File file = new File(strSignPath);
        if (!file.exists())
            file.mkdirs();

        if (file.exists())
            file.delete();

        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }

    public static byte[] Bitmap2Bytes(Bitmap bm){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public static String track2Json(String track){
        if (TextUtils.isEmpty(track)) return "";
        JSONObject jbOut = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        String[] points = track.split(",");
        for (int i = 0; i < points.length; i++) {
            String[] point = points[i].split(":");
            try {
                JSONObject jb = new JSONObject();
                jb.put("x",point[0]);
                jb.put("y",point[1]);
                jb.put("t",point[2]);
                jsonArray.put(i,jb);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return jsonArray.toString();
    }

    public static String json2XmlStr(){
        StringBuffer stringBuffer = new StringBuffer();
        JsonArray jsonArray  = new JsonArray();
        return stringBuffer.toString();
    }

}